package com.mjk.manager.magicapi.conf.provider;


import com.mjk.common.util.NumberUtils;
import com.mjk.manager.magicapi.core.modules.db.model.Page;
import com.mjk.manager.magicapi.core.modules.db.provider.PageProvider;
import org.ssssssss.script.runtime.RuntimeContext;

import java.util.Objects;

/**
 * 自定义获取分页参数
 * https://ssssssss.org/magic-api/pages/quick/page/
 *
 * @see PageProvider
 * @see org.ssssssss.magicapi.modules.db.provider.DefaultPageProvider
 */
public class CustomPageProvider implements PageProvider {

    @Override
    public Page getPage(RuntimeContext context) {
        // 从Request中提取page以及pageSize
        long page = NumberUtils.toLong(Objects.toString(context.eval("pageStart"), ""), 1);
        long pageSize = NumberUtils.toLong(Objects.toString(context.eval("pageSize"), ""), 10);
        return new Page(pageSize, (page - 1) * pageSize);
    }
}
